
(function(window) {
    // 关于顶部的这个提示：因不容易确定“当前最上层的win”，故给所有win发event。那些活着的win（即当前的win层）都会展示此提示。这并不影响体验，实现起来相对简单
    // 若用户主动关闭一个（或点开链接），则发全局event，关闭所有win中的此提示
    function listenToPushTipsEvent() {
        evt.listenTo('hot-push-tips', function(data) {
            showTipbar(data);
        });
        evt.listenTo('close-hot-push-tips', function(data) {
            closeTipbar();
        });
    }

    function showTipbar(payload) {
        window.showTipbar_payload = payload;
        var pid = 0;
        if (payload.action.postinfo){
            pid = payload.action.postinfo.pid
        } else if (payload.action.replyinfo) {
            pid = payload.action.replyinfo.pid;
        }
        var json = {
            tid: payload.articleid || 0,
            content: payload.content,
            pid: pid,
            pushid: payload.pushid
        };

        if ($api.byId('tips')) {
            closeTipbar();
        }

        var tpl = '<div id="tips" class="aui-tips">' +
        '<i class="aui-iconfont aui-icon-info"></i>' +
        '<div class="aui-tips-title aui-ellipsis-1" data-fid="-1" data-pos="{{=it.position}}" data-pid="{{=it.pid}}" data-tid="{{=it.tid}}" data-pushid="{{=it.pushid}}" data-from="push-tips" tapmode onclick="openPostFromTips(this);">' +
        '	{{=decodeEmoji(it.content)}}' +
        '</div>' +
        '<i class="aui-iconfont aui-icon-close" tapmode onclick="closeAllTips();"></i>' +
        '</div>';
        var render = doT.template(tpl);
        var html = render(json);
        $api.append($api.dom('body'), html)

        setTimeout(function() {
            closeTipbar();
        }, cfg.tips_show_time * 1000);

        sofast(); // 优化 tapmode
    }

    function closeTipbar() {
        var dom = $api.byId('tips');
        if (dom) {
            $api.remove(dom);
        }
    }

    function openPostFromTips(el) {
        // 添加新的在线推送处理方式
        if (window.showTipbar_payload && obj_being(window.showTipbar_payload.exec)) {
            try {
                var obj = JSON.parse(window.showTipbar_payload.exec);
                execCommad(obj.method, obj.params);
            }catch (e) {

            }
        }else {
            views.openPostFromList(el);
        }
        closeAllTips();
    }

    function closeAllTips() {
        evt.send('close-hot-push-tips', {});
    }

    window.listenToPushTipsEvent = listenToPushTipsEvent;
    window.openPostFromTips = openPostFromTips;
    window.closeAllTips = closeAllTips;
})(window);
